package emulator.shell.GUI.menu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import emulator.cbm.basic.BasicHelper;
import emulator.shell.DebugShell;
import emulator.shell.GUI.ConsoleFrame;
import emulator.util.file.FileHelper;
import emulator.util.file.FileInfo;

public class FileOpen implements ActionListener {

	private ConsoleFrame console;
	
	public FileOpen(ConsoleFrame consoleFrame) {
		console = consoleFrame;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		MemoryFileDialog dlg = new MemoryFileDialog(console.getFrame(), "Load", false);
		dlg.setVisible(true);
		if (dlg.isResultValid())
		{
			FileInfo info = FileHelper.getFileInfo(dlg.getFileName(), dlg.getFileFormat());
			if (info != null)
			{
				info.getLoadStrategy().loadToMemory(dlg.getFileName(),
													DebugShell.getInstance().getMemoryBlock(),
													dlg.getStartAddress(),
													dlg.getBlockSize());
				if (dlg.getRelinkBasic())
					BasicHelper.relinkProgram();
			}
			else
				JOptionPane.showMessageDialog(null, "File format not supported yet.");
		}
	}

}
